﻿using System.Web;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using System.Data;

using GUI.Extensions;



namespace GUI.Services
{
    /// <summary>
    /// Summary description for GetEmbedData
    /// </summary>
    public class GetEmbedData : IHttpHandler
    {



        public void ProcessRequest(HttpContext context)
        {
            var ajaxResponse = new AjaxResponse();

            var imageWidth = RequestExtensions.ImageWidth;
            var imageHeight = RequestExtensions.ImageHeight;
            var top = RequestExtensions.Top;
            var type = RequestExtensions.Type;



            var dt = NewsBO.GetInstance().GetEmbedNews(top: top, type: type);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (!dt.Columns.Contains("EmbedImage"))
                {
                    dt.Columns.Add("EmbedImage");
                }
                if (!dt.Columns.Contains("FormatedImage"))
                {
                    dt.Columns.Add("FormatedImage");
                }
                if (!dt.Columns.Contains("AbsoluteURL"))
                {
                    dt.Columns.Add("AbsoluteURL");
                }
                foreach (DataRow r in dt.Rows)
                {
                    r["AbsoluteURL"] =
                        PresentationUtils.UrlBuilder.BuildAbsoluteUrl(r[SqlConstants.NewsPublish.Url].ToString());
                    r["EmbedImage"] = imageHeight > 0 ? 
                        PresentationUtils.Photo.GetThumbImage_Zoom(r[SqlConstants.NewsPublish.Avatar].ToString(), imageWidth, imageHeight) :
                        PresentationUtils.Photo.GetThumbImage_W(r[SqlConstants.NewsPublish.Avatar].ToString(), imageWidth);
                    r["FormatedImage"] = PresentationUtils.Photo.BuildPhoto(
                        r[SqlConstants.NewsPublish.Avatar].ToString(),
                        r[SqlConstants.NewsPublish.Title].ToString(),
                        r["AbsoluteURL"].ToString(),
                        imageWidth, imageHeight);
                }
                ajaxResponse.Data = dt;
                ajaxResponse.Success = true;
            }
            var callback = !string.IsNullOrEmpty(context.Request.Params["callback"]) ? context.Request.Params["callback"] : "";
            var output = ajaxResponse.ToJson();
            if (callback != string.Empty) output = callback + "(" + output + ")";
            context.Response.Write(output);


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}